 /*
  *@(#)RequestQuery.java		8 Sept. 2000
  *
  * Copyright: MetaPlus Inc & Inquiry Management Systems Ltd.  2000
  *
  * This software is the proprietary information of MetaPlus Inc & 
  * Inquiry Management Systems Ltd.  
  * Use is subject to license terms.
  */
package com.ims.iaa;

/**
  * This class <code>RequestQuery</code> demonstrates how to query requests 
  * processing state through the inquiry interface provided by the
  * "Request Control Center". It aslo provided a useful approach to monitor
  * requests without the need of accessing database.
  *
  * @see RequestCtl#inquiryRequest
  *
  * @author Anthony Zhang
  *
  * @version 1.00, 25 Sept. 2000
  *
  */
public class RequestQuery {
	
	public static void main( String[] args ) {
		
		String serverName = "localhost";
		int serverPort = 1099;
		
		switch ( args.length ) {

		case 2 :
			try  { // try to get serverPort from command line
				serverPort = Integer.parseInt( args [1] );
			}catch (Exception e ) {
				usageInfo();
				return;
			}		
		case 1:
			try  { // try to get serverName from command line
				serverName = args [0];
			}catch (Exception e ) {
				usageInfo();
				return;
			}
			break;
		case 0: 
			break;
		default:
			usageInfo();
			
		}
		
		RequestCtl reqCenter = IaaUtil.getRequestCenter(serverName,serverPort);	
		if ( reqCenter == null) {
		    System.out.println( "Can not connect to Request Center" );		
			return;
		}
		
	    System.out.println( "Begin inquiry the Request Center" );
		
		Object[] adReqs;		
		try  {
			adReqs = reqCenter.inquiryRequest(AdTrackRequest.WAITING);
			for ( int i = 0; i < adReqs.length; i++) {
				System.out.println("Waiting:"+ i + " " + ((AdTrackRequest)adReqs[i]).getUrl());
			}

			adReqs = reqCenter.inquiryRequest(AdTrackRequest.PROCESSING);
			for ( int i = 0; i < adReqs.length; i++) {
				System.out.println("Process:"+ i + " " + ((AdTrackRequest)adReqs[i]).getUrl());
				System.out.println("   Depth:     " + ((AdTrackRequest)adReqs[i]).getDepth());			
				System.out.println("   Start:     " + ((AdTrackRequest)adReqs[i]).getStartTime());			
			}
			adReqs = reqCenter.inquiryRequest(AdTrackRequest.PROCESSED);
			for ( int i = 0; i < adReqs.length; i++) {
				System.out.println("Finished:"+ i + " " + ((AdTrackRequest)adReqs[i]).getUrl());
				System.out.println("   Depth:     " + ((AdTrackRequest)adReqs[i]).getDepth());			
				System.out.println("   Start:     " + ((AdTrackRequest)adReqs[i]).getStartTime());			
				System.out.println("   End  :     " + ((AdTrackRequest)adReqs[i]).getEndTime());			
			}

		} catch  (Exception e)  {
		    	System.out.println( "Communication with Request Center error " + e);			
		}	
		
		System.exit(0);
	}

	public static void usageInfo() {		
		    System.out.println( "usage: RequestQuery servername port " );
		    System.out.println( "	 servername: server host name or IP address," 
		    	+ " default is Localhost " );
		    System.out.println( "        port: port number of server rmiregistry port," 
		    	+ " default is 1099 " );		   		    
		    return;
	}

}		